package br.com.controlcivil.converter;

import java.math.BigDecimal;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("converterBigDecimal")
public class ConverterBigDecimal implements Converter{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String num) {
		
		if (num == "" || num == null){
			num = "0";
		}
		
		if (num != null){
			num = num.replace(",", ".");
		}
		
		BigDecimal decimal = new BigDecimal(num);
		return decimal;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object num) {
		BigDecimal decimal = (BigDecimal) num;		
		return decimal.toString();
	}

}
